//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Code Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// The following people are credited with originating all or parts of this code:
// FlashDoctors | flashdoctors.sourceforge.net
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.collections
{
	import org.as3commoncode.collections.ICollection;
	import org.as3commoncode.collections.IIterator;
	
	/**
	 * An abstract collection class.
	 * 
	 * @see	org.as3commoncode.collections.ICollection;
	 * @author Tony Birleffi
	 */
	public class AbstractCollection implements ICollection
	{
		/** * Instance of the ICollection. */
		protected var sub:ICollection;
		
		/** * An array variable. */
		public var arr:Array;
		
		/**
		 * Constructor.
		 */
		public function AbstractCollection()
	    {
	        sub = ICollection(this);
	        if(sub == null) throw new Error("Classes extending AbstractCollection must implement ICollection");
	        this.arr = new Array();
	    }	
		
		/**
		 * Set the collection to an array.
		 * 
		 * @return Array.
		 */
		public function toArray():Array 
		{
			var a:Array = new Array();
			var i:uint = 0;
			var s:uint = this.size();
			for(i = 0; i < s; i++){
				a.push(this.arr[i]);
			}
			return a;
		}
		
		/**
		 * If the collection is empty.
		 * 
		 * @return Boolean.
		 */
		public function isEmpty():Boolean 
		{
			return (this.arr.length == 0);
		}
		
		/**
		 * Contains the object.
		 * 
		 * @param obj	Object.
		 * @return Boolean.
		 */
		public function contains(obj:Object):Boolean 
		{
			var idx:int = this.findIndex(obj);
			return (idx > -1);
		}
		
		/**
		 * Contains all in the collection list.
		 * 
		 * @return Boolean.
		 */
		public function containsAll(list:ICollection):Boolean 
		{
			var it:IIterator = list.iterator();
			while(it.hasNext()){
				if(! this.contains(it.next())){
					return false;
				}
			}
			return true;
		}
		
		/**
		 * Add an item to the collection.
		 * 
		 * @param obj	Object.
		 */
		public function addItem(obj:Object):void 
		{
			this.arr.push(obj);
		}
		
		/**
		 * Add an entire collection list.
		 * 
		 * @param list	ICollection.
		 */
		public function addAll(list:ICollection):void 
		{
			var it:IIterator = list.iterator();
			while(it.hasNext()){
				this.arr.push(it.next());
			}
		}
		
		/**
		 * Remove the object from the collection.
		 * 
		 * @param obj	Object.
		 */
		public function remove(obj:Object):void 
		{
			var idx:int = this.findIndex(obj);
			if(idx){
				this.arr.splice(idx, 1);
			}
		}
		
		/**
		 * Clear out the collection.
		 */
		public function clear():void 
		{
			this.arr.splice(0,this.size());
		}
		
		/**
		 * Returns the size of the collection.
		 * 
		 * @return int;
		 */
		public function size():int
		{
			return this.arr.length;
		}
		
		/**
		 * Remove all from the collection list.
		 * 
		 * @param list	ICollection.
		 */
		public function removeAll(list:ICollection):void 
		{
			var it:IIterator = list.iterator();
			while(it.hasNext()){
				this.remove(it.next());
			}
		}
		
		/**
		 * Retain all from the collection list.
		 * 
		 * @param list	ICollection.
		 */
		public function retainAll(list:ICollection):void
		{
			var i:uint;
			var newArr:Array = new Array();
			var s:uint = this.size();
			for(i = 0; i < s; i++){
				if(list.contains(this.arr[i])){
					newArr.push(this.arr[i]);
				}
			}
			this.arr = newArr;
		}
		
		/**
		 * A string representation.
		 * 
		 * @return String.
		 */
		public function toString():String 
		{
			return this.arr.toString();
		}
		
		/**
		 * The iterator used.
		 * 
		 * @return IIterator.
		 */
		public function iterator():IIterator 
		{
			throw(new Error("Classes extending AbstractCollection must override iterator()"));
			return null;
		}
		
		/**
		 * Find the index of an object in the collection.
		 * 
		 * @param obj	Object.
		 * @return int.
		 */
		protected function findIndex(obj:Object):int
		{
			var i:int;
			var s:uint = this.size();
			for(i = 0; i < s; i++){
				if(this.arr[i] == obj){
					return i;
				}
			}
			return -1;
		}
	}
}